package top.hidoctor.auscultation.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

import com.tang.util.BigDecimalUtil;
import com.tang.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

import top.hidoctor.auscultation.R;


/**
 * Created by Administrator on 2015/10/13.
 */
public class EcgingView extends View {
    private Context context;
    private int widths;
    private int height;


    //灰色背景的画笔
    private Paint mPaint_bg;
    //折线的画笔
    private Paint mPaint_brokenline;
    //路径
    private Path mpath = new Path();
    //客户拜访的折线（BrokenLineCusVisit）数据
    private List<Float> mdata = new ArrayList<>();
    private int xori;//原点x坐标
    private float gap_x = 0f;

//    public void setGap_x(float gap_x) {
//        this.gap_x = gap_x;
//    }

    public int getWidths() {
        return widths;
    }

    public EcgingView(Context context) {
        super(context);
        this.context = context;
    }

    public EcgingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        inite(context);
    }


    private void inite(Context context) {


        mPaint_brokenline = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint_brokenline.setColor(context.getResources().getColor(R.color.blue_main));
        mPaint_brokenline.setTextSize(18);
        mPaint_brokenline.setTextAlign(Paint.Align.CENTER);

        invalidate();
    }

    public List<Float> getMdata() {
        return mdata;
    }

    public void setMdata(List<Float> list) {
        Log.e("TAG", "setMdata: " + list.size());
        mdata = list;
        requestLayout();//相当于调用onMeasure方法
        invalidate();//相当于调用onDraw方法
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < mdata.size(); i++) {
            if (i == 0) {
                //开始时需要将path移动到要开始绘制的位置，否则默认从（0,0）开始，绘制path路径
//                mpath.moveTo(0, height - height * mdata.get(i));
                mpath.moveTo(0, height - (float) (BigDecimalUtil.div(mdata.get(i) + 1, 2) * height));
            }
            if (i != mdata.size() - 1) {
                //根据圆点位置绘制折线
//                canvas.drawLine(gap_x * i, height - height * mdata.get(i), xori + gap_x * (i + 1), height - height * mdata.get(i + 1), mPaint_brokenline);
                canvas.drawLine(
                        gap_x * i + 1,
                        (float) (height - height * (BigDecimalUtil.div(mdata.get(i) + 1, 2))),
                        gap_x * (i + 1) + 1,
                        (float) (height - height * (BigDecimalUtil.div(mdata.get(i + 1) + 1, 2))),
                        mPaint_brokenline);
            }

            if (i == mdata.size() - 1) {
                //绘制完最后一个点，path需要沿着纵轴向下到达heigh - brokenline_bottom 的位置再水平到达（10，heigh - brokenline_bottom ）的位置，最后闭合
//                mpath.quadTo(gridspace_width * i + 10, height - height * mdata.get(i), gridspace_width * i + 10, height);
//                mpath.quadTo(gridspace_width * i + 10, height, 10, height);
//                mpath.quadTo(gridspace_width * i, height, 0, height);
                mpath.close();
            }
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        xori = 0;//原点x坐标
        //两分钟一屏
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        int width = displayMetrics.widthPixels - DensityUtil.dip2px(context, 40);
        if (width > 0 && mdata.size() > 0) {
            gap_x = (float) BigDecimalUtil.div(width, mdata.size());
        }
        if (mdata.size() == 0) {
            //通过调用onMeasure源码中的方法进行获得宽度，
            //宽度的获得有三种模式，具体介绍见博客底部连接
//            widths = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
            widths = widthMeasureSpec;
        } else {
            //根据数据条数设置宽度
            widths = (int) (gap_x * mdata.size());
        }
        height = 400;
//        yLine = widths / (int) gridspace_width;
        //绘制网格线，横向的；Y轴不变 X轴绘制直线
//        xLine = height / (int) gridspace_width; //橫軸條數
//        gridspace_heigh = height / xLine;
        setMeasuredDimension(widths, height);
    }


}